package com.xcfw.dorm.seviceimpl.dormassistant;

import com.xcfw.dorm.entity.*;
import com.xcfw.dorm.exception.ServiceException;
import com.xcfw.dorm.mapper.DormAssistanMapper;
import com.xcfw.dorm.mapper.LeaveMapper;
import com.xcfw.dorm.mapper.StudentMapper;
import com.xcfw.dorm.sevice.BaseService;
import com.xcfw.dorm.util.DormUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @program: Dorm
 * @description: 宿管服务层
 * @author: Bruse Queen
 * @create: 2018-04-16 22:08
 **/
@Service("dormManageServiceImpl")
public class DormManageServiceImpl extends BaseService {
    @Autowired
    private LeaveMapper leaveMapper;
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private DormAssistanMapper dormAssistanMapper;

    public ReturnJson selectBuildingNum(String building) {
        // 根据公寓号查询学生数据
        int studentNum = studentMapper.selectLogin(null, null, null, building);
        // 查询请假记录
        List<Leave> leaves = leaveMapper.selectActionSql(null, null,
                null, null, null,null);

        // 获取现在的时间
        Date date = new Date();
        DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
        String now = dateFormat.format(date);
        Integer nowInteger = Integer.parseInt(now);

        // 集合不为空
        if (leaves.size() != 0) {
            Iterator<Leave> iterator = leaves.iterator();
            while (iterator.hasNext()) {
                Leave l = iterator.next();
                // 比较请假的时间
                if (DormUtil.getInteget(l.getBeginTime()) > nowInteger || nowInteger >= DormUtil.getInteget(l.getEndTime())) {
                    iterator.remove();
                }
            }
        }

        Map<String, String> map = new HashMap<String, String>(3);
        map.put("totalSudent", String.valueOf(studentNum));
        map.put("leaveStudent", String.valueOf(leaves.size()));
        map.put("shouldStudent", String.valueOf(studentNum - leaves.size()));

        return new ReturnJson(NumEnum.SUCCESS.getIndex(), "公寓学生入住信息查询成功！", map);
    }

    public ReturnJson getExcelJson(Student student) {
        List<Map<String, String>> list = new ArrayList<>();
        list = studentMapper.selectExcel(null, null, null, student.getBuilding(),
                null, null, null, null);
        if (list.size() != 0) {
            List<Map> listFloor = new ArrayList<>();
            String dormnumber = "";
            for (int i = 1; i <= 6; i++) {
                Map<String, Object> mapFloor = new HashMap<>();
                List<Map> listDrom = new ArrayList<>();
                for (int j = 1; j <= 25; j++) {
                    Map<String, Object> mapDorm = new HashMap<>();
                    List<Map> listStudent = new ArrayList<>();
                    for (Map<String, String> m : list) {
                        dormnumber = String.valueOf(i + String.valueOf(j > 9 ? j : "0" + j));
                        if (dormnumber.equals(String.valueOf(m.get("DORMITORY")))) {
                            Map<String, String> mapStudent = new HashMap<>();
                            mapStudent.put("sudentName", m.get("NAME"));
                            mapStudent.put("className", m.get("STUDENT_CLASS"));
                            listStudent.add(mapStudent);
                        }
                    }
                    mapDorm.put("students", listStudent);
                    mapDorm.put("dormNumber", dormnumber);
                    listDrom.add(mapDorm);
                }
                mapFloor.put("dormNumber", listDrom);
                mapFloor.put("floorNumber", i);
                listFloor.add(mapFloor);
            }

            return new ReturnJson(NumEnum.SUCCESS.getIndex(), "公寓平面图生成成功！", listFloor);
        } else {
            throw new ServiceException("暂无相关数据！");
        }
    }

    public ReturnJson changeDormAssistantPassword(DormAssistan dormAssistan) {
        List<DormAssistan> list = dormAssistanMapper.selectActionSql(String.valueOf(dormAssistan.getSchoolId()), null, null, dormAssistan.getIdcard());
        if (list.size() == 1) {
            dormAssistan.setId(list.get(0).getId());
            dormAssistanMapper.updateByPrimaryKeySelective(dormAssistan);
            return new ReturnJson(NumEnum.SUCCESS.getIndex(), "修改密码成功！");
        } else {
            return new ReturnJson(NumEnum.FAIL.getIndex(), "修改密码失败！");
        }
    }

}



